package com.sskj.jgxt.impl;

import java.util.HashMap;
import java.util.Map;

import javax.transaction.Transactional;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.sskj.core.exception.MException;
import com.sskj.jgxt.RestParamVO;
import com.sskj.jgxt.RestQueryVO;
import com.sskj.jgxt.param.Param;
import com.sskj.jgxt.param.WeChatSysQueryParam;
import com.sskj.jgxt.user.vo.PersonVO;
import com.sskj.utils.HttpUtils;
import com.sskj.wechat.domain.WeChatUserQueryParam;
import com.sskj.wechat.domain.entity.WeChatUserEntity;
import com.sskj.wechat.repository.WeChatUserRepository;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

@RestController
@RequestMapping(value = "/wechat/sys", method = { RequestMethod.POST, RequestMethod.GET })
@Transactional
public class WeChatSysServiceImpl {

	private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Value("${sskj.conf.restSysUrl}")
	private String restSysUrl;
	
	@Value("${sskj.conf.restUrl}")
	private String restUrl;	
	
	@Autowired
	WeChatUserRepository weChatUserRepository;	
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/getwtuser")
	@ApiOperation(value = "查询微信关注用户信息", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "openId", value = "openId", paramType = "form", dataType = "string") })
	public WeChatUserEntity getWTuser(@ApiIgnore WeChatUserQueryParam weChatUserQueryParam) {
		
		WeChatUserEntity weChatUserEntity = weChatUserRepository.findBindingUserByOpenId(weChatUserQueryParam.getOpenId());

		return weChatUserEntity;
	}
	
	
	
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/getjduser")
	@ApiOperation(value = "查询微信关注用户信息", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "openId", value = "openId", paramType = "form", dataType = "string") })
	public JSONObject getJDuser(@ApiIgnore WeChatUserQueryParam weChatUserQueryParam) {
		
		
				try {
				
					RestQueryVO data = new RestQueryVO();
					
					RestParamVO param = new RestParamVO();
					param.setPn("phone");
					if (weChatUserQueryParam.getOpenId().isEmpty()) {
						param.setValue("13988888888");
					} else {
						param.setValue(weChatUserQueryParam.getOpenId());
					}
					data.getParams().add(param);					
					
					
					
					JSONObject obj = HttpUtils.httpPostOne(restUrl + "jd/weixin/getPersonInfo", data);
					if (obj != null) {
						System.out.println(
								"JSONObject code=" + obj.toJSONString());						
						return obj;
					}															
				} catch (Exception e) {
					
					e.printStackTrace();
					
					throw new MException("request_timeout");
					
				}						

				
	
		return null;
	}	
	
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/getwtcertuser")
	@ApiOperation(value = "查询微信关注用户证书信息", httpMethod = "GET")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "openId", value = "openId", paramType = "form", dataType = "string") })
	public JSONObject getWtCertUser(@ApiIgnore WeChatUserQueryParam weChatUserQueryParam) {
		
		WeChatUserEntity weChatUserEntity = weChatUserRepository.findBindingUserByOpenId(weChatUserQueryParam.getOpenId());
		if(weChatUserEntity!=null){
			try {
				Param data = new Param();
				data.setNo(weChatUserEntity.getTelNum());
				data.setText(weChatUserEntity.getCertNum());
				try {
							
					JSONObject obj = HttpUtils.httpPostOne(restSysUrl + "sys/weixin/getPersonInfo", data);
					if (obj != null) {
						System.out.println(
								"JSONObject code=" + obj.toJSONString());
						return obj;
					}
				} catch (Exception e) {
					
					e.printStackTrace();
					
					throw new MException("request_timeout");
					
				}						

			} catch (Exception e) {
				e.printStackTrace();			
				throw new MException("request_timeout");
		
			}				
			
				
		}
		return null;
	}	
	
	
	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/checkJDPhone")
	@ApiOperation(value = "获取人员信息记录", notes = "获取人员信息记录", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "certnum", value = "certnum", paramType = "form", dataType = "string") })	                    
	public JSONObject checkJDPhone(@ApiIgnore WeChatSysQueryParam p) {
		
		System.out.println(
				"p code=" + p.getCertnum());
		try {
			RestQueryVO data = new RestQueryVO();
			
			RestParamVO param = new RestParamVO();
			param.setPn("phone");
			if (p.getCertnum().isEmpty()) {
				param.setValue("13988888888");
			} else {
				param.setValue(p.getCertnum());
			}
			data.getParams().add(param);
			
			//data.setNo(p.getCertnum()); 
			try {
						
				JSONObject obj = HttpUtils.httpPostOne(restUrl + "sys/meguser/", data);
				if (obj != null) {
					System.out.println(
							"JSONObject code=" + obj.toJSONString());
					return obj;
				}
			} catch (Exception e) {
				
				e.printStackTrace();
				
				throw new MException("request_timeout");
				
			}						

		} catch (Exception e) {
			e.printStackTrace();			
			throw new MException("request_timeout");
	
		}	
		return null;
		
	}	
	

	@CrossOrigin(origins = "*", maxAge = 3600)
	@RequestMapping(value = "/checkCert")
	@ApiOperation(value = "获取人员信息记录", notes = "获取人员信息记录", httpMethod = "GET")
	@ApiImplicitParams({ @ApiImplicitParam(name = "certnum", value = "certnum", paramType = "form", dataType = "string") })	                    
	public JSONObject checkCert(@ApiIgnore WeChatSysQueryParam p) {
		
		System.out.println(
				"p code=" + p.getCertnum());
		try {
			Param data = new Param();
			data.setNo(p.getCertnum()); 
			try {
						
				JSONObject obj = HttpUtils.httpPostOne(restSysUrl + "sys/weixin/getPersonByCertNumber", data);
				if (obj != null) {
					System.out.println(
							"JSONObject code=" + obj.toJSONString());
					return obj;
				}
			} catch (Exception e) {
				
				e.printStackTrace();
				
				throw new MException("request_timeout");
				
			}						

		} catch (Exception e) {
			e.printStackTrace();			
			throw new MException("request_timeout");
	
		}	
		return null;
		
	}
	
	
	
	public PersonVO getSysUser(Map<String, String> querys) {
		
		String path = "sys/weixin/getPersonInfo";
		String method = "GET";		
		Map<String, String> headers = new HashMap<String, String>();		
		try {
			HttpResponse response = HttpUtils.doGet(restSysUrl, path, method, headers, querys);

			if (response.getStatusLine().getStatusCode() != 200) {
				System.out.println(
						"request url failed, http code=" + response.getStatusLine().getStatusCode() + ", url=" + restSysUrl);
				return null;
			}
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String resultStr = EntityUtils.toString(entity, "utf-8");

				return null;

			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		return null;
	}		

	



}
